<?php

namespace App\Http\Controllers;

use App\Http\Resources\HelpResources\NoticeListResource;
use App\Http\Resources\HelpResources\FeedbacklistResource;
use App\Models\Notice;
use App\Models\Feedback;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class HelpController extends Controller
{
    /**
     * 公告列表.
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function notice(Request $request)
    {
        $lang = $request->header('Language', 'ZH-CN');
        $per_page = $request->input('per_page');
        $noticeList = Notice::orderBy('id', 'desc')
            ->when('EN-US' == $lang, function ($query) { // 英文公告
                $query->where('lang', 2);
            })
            ->when('EN-US' != $lang, function ($query) { // 中文公告
                $query->where('lang', 1);
            })
            ->paginate($per_page);

        return NoticeListResource::collection($noticeList);
    }
    /**
     * 公告列表.
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function feedback(Request $request)
    {
        $noticeList = new Feedback;
        $noticeList->name = $request->get('name');
        $noticeList->file = $request->get('file');
        $noticeList->uid = Auth::id();
        $noticeList->content = $request->get('content');
        $noticeList->replay = $request->get('replay');

        $noticeList->save();
        return $this->null();
    }
    /**
     * 公告列表.
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function feedbacklist(Request $request)
    {
        // $lang = $request->header('Language', 'ZH-CN');
        $per_page = $request->input('per_page');
        $noticeList = Feedback::orderBy('id', 'desc')->where('uid', Auth::id())
            ->paginate($per_page);

        return FeedbacklistResource::collection($noticeList);
    }
    /**
     * 公告列表.
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function feedbackdetail(Request $request)
    {
        $noticeList = Feedback::where("id", $request->query('id'))->where('uid', Auth::id())
            ->get();

        return FeedbacklistResource::collection($noticeList);
    }
    /**
     * 公告列表.
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function detail(Request $request)
    {
        $lang = $request->header('Language', 'ZH-CN');
        $id = $request->get('id', '');
        $noticeList = Notice::orderBy('id', 'desc')
            ->when('EN-US' == $lang, function ($query) { // 英文公告
                $query->where('lang', 2);
            })
            ->when('EN-US' != $lang, function ($query) { // 中文公告
                $query->where('lang', 1);
            })
            ->where("id", $id)->get();

        return NoticeListResource::collection($noticeList);
    }
}
